Skip to content

Comments

feat: add HOST environment variable for configurable bind address#360

Merged
blackmammoth merged 4 commits intositeboon:mainfrom
mjfork:feature/host-binding
Feb 20, 2026
Merged

feat: add HOST environment variable for configurable bind address#360
blackmammoth merged 4 commits intositeboon:mainfrom
mjfork:feature/host-binding

Conversation

@mjfork
Copy link
Contributor

@mjfork mjfork commented Jan 31, 2026

Summary

  • Add HOST environment variable to configure which host/IP the servers bind to
  • Default to 0.0.0.0 (all interfaces) for backward compatibility
  • Update both Express server and Vite dev server to respect the HOST setting

Fixes #359

Changes

  • .env.example: Added HOST environment variable with documentation
  • server/index.js: Use HOST env var instead of hardcoded '0.0.0.0'
  • vite.config.js: Added host option to Vite server config

Test plan

  • Set HOST=127.0.0.1 and verify server only listens on localhost
  • Leave HOST unset and verify server listens on all interfaces (default behavior)
  • Verify both backend server and Vite dev server respect the HOST setting

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Server and development tooling now respect configurable HOST and PORT environment variables, so bind address and port can be changed without modifying code.
    • Local URLs shown in logs and dev proxies normalize a bind-all address to "localhost" for clearer, user-friendly links.
    • Dev proxy targets now follow the configured host/port so local proxies align with the chosen network binding.

Allow users to specify which host/IP address the servers should bind to
via the HOST environment variable. Defaults to 0.0.0.0 (all interfaces)
to maintain backward compatibility.

This enables users to restrict the server to localhost only (127.0.0.1)
for security purposes or bind to a specific network interface.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 31, 2026

📝 Walkthrough

Walkthrough

Adds a configurable HOST environment variable (default 0.0.0.0), maps 0.0.0.0 to localhost for display/proxy purposes, and updates server startup, Vite dev server host, and proxy targets to use the configured host/port.

Changes

Cohort / File(s) Summary
Environment Example
\.env.example
Added HOST entry with default 0.0.0.0 and comments explaining binding behavior and localhost mapping.
Server
server/index.js
Reads HOST from env (default 0.0.0.0); added DISPLAY_HOST mapping 0.0.0.0localhost; uses HOST for server.listen and updates startup URL log to use DISPLAY_HOST.
Vite Dev Server & Proxies
vite.config.js
Reads HOST and PORT from env (defaults 0.0.0.0/3001); computes proxyHost (localhost when binding to 0.0.0.0); sets server.host and updates /api, /ws, /shell proxy targets to use computed host/port.

Sequence Diagram(s)

sequenceDiagram
    participant Browser
    participant Vite as Vite Dev Server (proxy)
    participant Backend as App Server
    Browser->>Vite: HTTP/WS request
    Vite->>Vite: compute proxyHost from HOST env
    Vite->>Backend: Proxy request to http/ ws://{proxyHost}:{PORT}
    Backend-->>Vite: Response
    Vite-->>Browser: Response
Loading

Estimated code review effort

🎯 Low | ⏱️ ~10 minutes

Poem

🐇 I nibble bytes and hop on logs,
HOST set kindly, no more clogs,
From 0.0.0.0 I show as 'localhost',
Proxies route where I am most,
A tiny carrot for every running cog 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a HOST environment variable for configurable bind address, which directly aligns with the primary objective of the pull request.
Linked Issues check ✅ Passed All three required code changes from issue #359 are implemented: HOST added to .env.example with documentation, server/index.js updated to use HOST, and vite.config.js updated to respect HOST for the Vite dev server.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the configurable HOST binding feature as specified in issue #359; no extraneous modifications were introduced.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
vite.config.js (1)

11-22: ⚠️ Potential issue | 🟠 Major

Proxy targets ignore HOST and can break non-local binds.
If HOST is set to a specific interface (e.g., 192.168.1.100), the backend binds only there, but the Vite proxy still targets localhost, which will fail. Use a derived proxyHost that falls back to localhost only when HOST=0.0.0.0.

🐛 Proposed fix
 export default defineConfig(({ command, mode }) => {
   // Load env file based on `mode` in the current working directory.
   const env = loadEnv(mode, process.cwd(), '')
+  const host = env.HOST || '0.0.0.0'
+  const proxyHost = host === '0.0.0.0' ? 'localhost' : host
   
   return {
     plugins: [react()],
     server: {
-      host: env.HOST || '0.0.0.0',
+      host,
       port: parseInt(env.VITE_PORT) || 5173,
       proxy: {
-        '/api': `http://localhost:${env.PORT || 3001}`,
+        '/api': `http://${proxyHost}:${env.PORT || 3001}`,
         '/ws': {
-          target: `ws://localhost:${env.PORT || 3001}`,
+          target: `ws://${proxyHost}:${env.PORT || 3001}`,
           ws: true
         },
         '/shell': {
-          target: `ws://localhost:${env.PORT || 3001}`,
+          target: `ws://${proxyHost}:${env.PORT || 3001}`,
           ws: true
         }
       }
     },
🧹 Nitpick comments (1)
server/index.js (1)

1801-1829: Consider a user-friendly URL when HOST is 0.0.0.0.
Line 1829 will print http://0.0.0.0:PORT, which isn’t a connectable URL for most users. A display host (e.g., localhost when bound to all interfaces) avoids confusion.

♻️ Suggested tweak
-const HOST = process.env.HOST || '0.0.0.0';
+const HOST = process.env.HOST || '0.0.0.0';
+const DISPLAY_HOST = HOST === '0.0.0.0' ? 'localhost' : HOST;
...
-            console.log(`${c.info('[INFO]')} Server URL:  ${c.bright('http://' + HOST + ':' + PORT)}`);
+            console.log(`${c.info('[INFO]')} Server URL:  ${c.bright('http://' + DISPLAY_HOST + ':' + PORT)}`);

When HOST is set to a specific interface (e.g., 192.168.1.100), the Vite
proxy needs to connect to that same host, not localhost. This fix:

- Adds proxyHost logic that uses localhost when HOST=0.0.0.0, otherwise
  uses the specific HOST value for proxy targets
- Adds DISPLAY_HOST to show a user-friendly URL in console output
  (0.0.0.0 isn't a connectable address)

Addresses CodeRabbit review feedback.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@blackmammoth blackmammoth self-assigned this Feb 2, 2026
Copy link
Collaborator

@blackmammoth blackmammoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed and tested.

@blackmammoth blackmammoth merged commit cccd915 into siteboon:main Feb 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Allow configurable HOST/IP binding

2 participants